﻿Imports System.Guid
Public Class Animaux_CTRL
    Implements IDisposable

    Private _animalCourant As Animaux
    Private _index As Integer = 0
    Private _listeAnimaux As List(Of Animaux)
    Private _clientCourant As Clients
    Private _EDM As AniFormEntities
    Private _selectedEspece As String


#Region "Cycle de vie du controleur ANIMAUX"

    ''' <summary>
    ''' Constructeur du controleur ANIMAUX (on lui passe l'EDM)
    ''' </summary>
    ''' <param name="EDM"></param>
    ''' <param name="clientCourant"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal EDM As AniFormEntities, ByVal clientCourant As Clients)

        _EDM = EDM
        _clientCourant = clientCourant
        _listeAnimaux = New List(Of Animaux)
        _listeAnimaux.AddRange(_clientCourant.listeAnimauxClient)
        If _listeAnimaux.Count = 0 Then
            _animalCourant = Nothing
        Else
            _animalCourant = _listeAnimaux(_index)
        End If


    End Sub

    Private disposedValue As Boolean = False

    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then

            End If
            _index = Nothing
            _selectedEspece = Nothing
            _animalCourant = Nothing
            For Each element In _listeAnimaux
                element = Nothing
            Next
            _listeAnimaux.Clear()
            _listeAnimaux = Nothing
            _clientCourant = Nothing
            _EDM.Dispose()

        End If
        Me.disposedValue = True
    End Sub


    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

#End Region

    ''' <summary>
    ''' Propriete permetant de modifier / renvoyer le client courant
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property clientCourant() As Clients
        Get
            Return _clientCourant
        End Get
        Set(ByVal value As Clients)
            _clientCourant = value
        End Set
    End Property

    ''' <summary>
    ''' Propriete permetant de renvoyer l'animal courant
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property animalCourant() As Animaux
        Get
            If _listeAnimaux.Count > 0 Then
                _animalCourant = _listeAnimaux(_index)
                Return _animalCourant
            Else
                Return Nothing
            End If
        End Get
    End Property

    ''' <summary>
    ''' Propriete retournant la liste d'espece (string)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property listeEspece() As IEnumerable(Of String)
        Get
            Dim requete As IEnumerable(Of String) = From race In _EDM.Races Select race.Espece Distinct
            Return requete.AsEnumerable
        End Get
    End Property

    ''' <summary>
    ''' Propriete retournant la liste de race (string)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ListeRace() As List(Of String)
        Get
            Dim liste As New List(Of String)
            Dim requete As IEnumerable(Of Races) = (From uneRace In _EDM.Races Select uneRace Where uneRace.Espece = _selectedEspece)
            For Each element In requete.AsEnumerable
                liste.Add(element.Race)
            Next
            Return liste
        End Get
    End Property

    ''' <summary>
    ''' Propriete permetant de modifier / renvoyer l'espece selectionnée (pour déterminer la liste de race à afficher)
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property selectedEspece() As String
        Get
            Return _selectedEspece
        End Get
        Set(ByVal value As String)
            _selectedEspece = value
        End Set
    End Property

    ''' <summary>
    ''' Propriete d'index de navigation permetant de le modifier / recuperer
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property index() As Integer
        Get
            Return _index
        End Get
        Set(ByVal value As Integer)
            _index = value
        End Set
    End Property

    ''' <summary>
    ''' Propriete retournant la liste d'animaux du client courant
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property listeAnimaux() As IEnumerable(Of Animaux)
        Get
            Return _listeAnimaux
        End Get
    End Property

    ''' <summary>
    ''' Procedure d'archivage de l'animal courant
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub archiverAnimal()

        'archivage de l'animal courant
        _animalCourant.archiverAnimal()
        _listeAnimaux.Remove(_animalCourant)
        _EDM.SaveChanges(True)

    End Sub

    ''' <summary>
    ''' Procedure de modification de l'animal courant
    ''' </summary>
    ''' <param name="nom"></param>
    ''' <param name="couleur"></param>
    ''' <param name="sexe"></param>
    ''' <param name="tatouage"></param>
    ''' <remarks></remarks>
    Public Sub modifAnimal(ByVal nom As String, ByVal couleur As String, ByVal sexe As String, ByVal tatouage As String)

        _animalCourant.modifierAnimal(nom, couleur, sexe, tatouage)
        _EDM.SaveChanges(True)

    End Sub

    ''' <summary>
    ''' Procedure d'ajout d'un nouvel animal
    ''' </summary>
    ''' <param name="nom"></param>
    ''' <param name="couleur"></param>
    ''' <param name="race"></param>
    ''' <param name="espece"></param>
    ''' <param name="sexe"></param>
    ''' <param name="tatouage"></param>
    ''' <remarks></remarks>
    Public Sub ajoutAnimal(ByVal nom As String, ByVal couleur As String, ByVal race As String, ByVal espece As String, ByVal sexe As String, ByVal tatouage As String)


        Dim raceAnim As Races
        Dim verifRace As New List(Of Races)
        verifRace.AddRange((From raceExistante In _EDM.Races Select raceExistante Where raceExistante.Race = race And raceExistante.Espece = espece).AsEnumerable)

        'Si la race n'existe pas on la rajoute en BDD
        'Avant d'enregistrer l'animal
        If verifRace.Count = 0 Then
            raceAnim = Races.CreateRaces(race, espece)
            _EDM.AddToRaces(raceAnim)
            _EDM.SaveChanges(True)
        End If

        Dim animalAajouter As New Animaux

        animalAajouter = Animaux.CreateAnimaux(NewGuid, nom, sexe, False)

        animalAajouter.Clients = _clientCourant

        animalAajouter.Couleur = couleur
        animalAajouter.Tatouage = tatouage
        animalAajouter.Archive = False

        animalAajouter.Races = (From raceAnimal In _EDM.Races Select raceAnimal Where raceAnimal.Race = race And raceAnimal.Espece = espece).First

        _listeAnimaux.Add(animalAajouter)
        _EDM.AddToAnimaux(animalAajouter)
        _EDM.SaveChanges(True)

    End Sub

End Class
